﻿import { Component, EventEmitter, Injector, Output, ViewChild } from '@angular/core';
import { AppComponentBase } from '@shared/common/app-component-base';
import { PasswordComplexitySetting, ProfileServiceProxy, UserEditDto, UserServiceProxy, StandardPartsDetailDto } from '@shared/service-proxies/service-proxies';
import { ModalDirective } from 'ngx-bootstrap';
import * as _ from 'lodash';
import { finalize } from 'rxjs/operators';
import { RequirementPackageServiceProxy, RequirementItemDto, StandardPartsFeatureDto, UsableRangeDto, RequirementPackageDto } from '@shared/service-proxies/service-proxies';
import * as moment from 'moment';
import { StandardPartsDetailServiceProxy, DepartmentServiceProxy } from '@shared/service-proxies/service-proxies';
import { StandardPartsDetailSelf } from './standard-parts-detail_self';
@Component({
    selector: 'requirementItemEditComponent',
    templateUrl: './requirement-item_edit.component.html',
    styleUrls: ['./requirement-item_edit.component.less']

})
export class RequirementItemEditComponent extends AppComponentBase {

    @ViewChild('createOrEditModal') modal: ModalDirective;

    @Output() modalSave: EventEmitter<any> = new EventEmitter<any>();

    active = false;
    saving = false;
    passwordComplexitySetting: PasswordComplexitySetting = new PasswordComplexitySetting();
    formData: RequirementItemDto = new RequirementItemDto();
    standranPartsDetail: StandardPartsDetailDto = new StandardPartsDetailDto();
    user: UserEditDto = new UserEditDto();
    passwordComplexityInfo = '';
    profilePicture: string;
    planDate: Date = new Date();
    rquirementPackageDto: RequirementPackageDto;
    // 标准件编码字段选择-----------------------------
    // 标准件
    standardPartsDetailIdAuto: StandardPartsDetailSelf = new StandardPartsDetailSelf()
    // 全部标准件
    standardPartsDetailAll: any[];
    // 帅选标准件
    standardPartsFiltered: StandardPartsDetailSelf[];

    unitOfUsings: Array<object>;;
    filter(event): void {
        this.standardPartsFiltered = [];
        for (let i = 0; i < this.standardPartsDetailAll.length; i++) {
            let info: StandardPartsDetailSelf = new StandardPartsDetailSelf(this.standardPartsDetailAll[i]);

            let temp = event.query.toLowerCase();
            if (info.id.toLowerCase().indexOf(temp) >= 0) {
                this.standardPartsFiltered.push(info);
            } else if (info.features.displayName.toLowerCase().indexOf(temp) >= 0 || info.features.specification.toLowerCase().indexOf(temp) >= 0 || info.features.standardNumber.toLowerCase().indexOf(temp) >= 0 || info.features.materials.toLowerCase().indexOf(temp) >= 0) {
                this.standardPartsFiltered.push(info);
            }
        }
    }
    rqeusetStandardPartsDetailAll(): void {
        this.primengTableHelper.showLoadingIndicator();
        this._standardPartsDetailServiceProxy.getAllDetail("", null, 1000, 0)
            .pipe(finalize(() => this.primengTableHelper.hideLoadingIndicator())).subscribe(result => {
                this.standardPartsDetailAll = result.items;
            });
    }

    constructor(
        injector: Injector,
        private _userService: UserServiceProxy,
        private _departmentServiceProxy: DepartmentServiceProxy,
        private _requirementPackageServiceProxy: RequirementPackageServiceProxy,
        private _standardPartsDetailServiceProxy: StandardPartsDetailServiceProxy,
        private _profileService: ProfileServiceProxy
    ) {
        super(injector);
        this.getunitOfUsings();
    }
    getunitOfUsings() {
        this.unitOfUsings = new Array<object>()
        this._departmentServiceProxy.getAllDpts()
            .pipe(finalize(() => this.primengTableHelper.hideLoadingIndicator())).subscribe(result => {
                this.unitOfUsings = result;
            });
    }
    show(_pRquirementPackageDto: RequirementPackageDto, requirementItemDto?: RequirementItemDto): void {
        this.rquirementPackageDto = _pRquirementPackageDto;

        if (requirementItemDto) {
            this._standardPartsDetailServiceProxy.getDetail(requirementItemDto.standardPartsDetailId)
            .subscribe((result) => { 
                this.standranPartsDetail.serialNumber = result.serialNumber ;

                this.formData = requirementItemDto;
                this.formData.requirementPackageId = this.rquirementPackageDto.id;
                this.planDate = requirementItemDto.requiredTimeOfModel.toDate();
                this.standardPartsDetailIdAuto = new StandardPartsDetailSelf()
                this.standardPartsDetailIdAuto.id = requirementItemDto.standardPartsDetailId;
                this.standardPartsDetailIdAuto.serialNumber = this.standranPartsDetail.serialNumber;
                this.standardPartsDetailIdAuto.features = requirementItemDto.features;
            });
            
        } else {
            this.formData = new RequirementItemDto();
            this.formData.requirementPackageId = this.rquirementPackageDto.id;
            this.formData.features = new StandardPartsFeatureDto();
            this.formData.usableRange = new UsableRangeDto();
            this.standardPartsDetailIdAuto = new StandardPartsDetailSelf()
            this.planDate = new Date();
        }
        this.active = true;
        this.modal.show();
        this.rqeusetStandardPartsDetailAll();
    }

    onShown(): void {

    }
    /**
     * 需求详细添加保存
     * 需求详细编辑保存
     */
    save(): void {
        this.saving = true;
        this.formData.features = this.standardPartsDetailIdAuto.features;
        this.formData.standardPartsDetailId = this.standardPartsDetailIdAuto.id;
        if (!this.formData.id) {
            // 日期转换
            this.formData.requiredTimeOfModel = moment(this.planDate)
            this._requirementPackageServiceProxy.createItemsOfPackage(this.formData)
                .pipe(finalize(() => { this.saving = false; }))
                .subscribe(() => {
                    this.notify.info(this.l('SavedSuccessfully'));
                    this.close();
                    this.modalSave.emit(null);
                });
        } else {
            // 日期转换
            this.formData.requiredTimeOfModel = moment(this.planDate)
            this._requirementPackageServiceProxy.updateItemsOfPackage(this.formData)
                .pipe(finalize(() => { this.saving = false; }))
                .subscribe(() => {
                    this.notify.info(this.l('SavedSuccessfully'));
                    this.close();
                    this.modalSave.emit(null);
                });
        }
    }

    close(): void {
        this.active = false;
        this.modal.hide();
    }
}
